home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / comm / uptime / part01 / Uptime.mod next >
Text File  |  1990-10-14  |  4KB  |  115 lines

  1. MODULE Uptime;
  2. (*
  3.   how long has CD on serial device been active?
  4.   Stefan H-M Ludwig, Englischviertelstr. 47, CH-8032 Zuerich
  5.   sludwig@iiic.ethz.ch,        cernvax!forty2!tll!ludwig
  6.   3 Sep 1990
  7.   Use "Run Uptime" to start it
  8.   Use "BREAK <process#>" to end it
  9.   Use "uptime ?" to see, how long CD has been active since last reset
  10.   Delete "S:Uptime.txt" to reset
  11. *)
  12.  
  13. IMPORT
  14.   Arguments, Arts, ASCII, Dos, Exec, FileSystem, Hardware, SYSTEM;
  15.  
  16. FROM InOut IMPORT Write, WriteCard, WriteString, WriteLn;
  17.  
  18. CONST
  19.   delay = 100; (*2 secs @ 50, 1.67 @ 60 ticksPerSec*)
  20.   fileName = "S:Uptime.txt";
  21.   bufSize = SIZE (Dos.Date)+SIZE (LONGINT); (*creation + uptime*)
  22.   
  23. VAR
  24.   old, new, creation: Dos.Date;
  25.   oldPtr, newPtr, creationPtr: Dos.DatePtr;
  26.   file: FileSystem.File;
  27.   ticksPerSec, ticksPerHalf,
  28.   ticksPerMin, ticksPerHour, ticksPerDay, uptime: LONGINT;
  29.   
  30. (*
  31. PROCEDURE CD (): BOOLEAN; (*is CD of serial active?*)
  32. BEGIN (*CD*)
  33.   (*DoIO and cmdQuery would be cleaner :-)*)
  34.   RETURN ~(Hardware.comCD IN Hardware.ciab.pra); (*CD is active low!!*)
  35. END CD;
  36. *)
  37.  
  38. PROCEDURE WriteEsc (str: ARRAY OF CHAR);
  39. BEGIN (*WriteEsc*)
  40.   Write (33C); Write ("["); WriteString (str); Write ("m");
  41. END WriteEsc;
  42.   
  43. BEGIN (*Uptime*)
  44.   (*uptime := 0, done by M2*)
  45.   Arts.DetectCtrlC (FALSE);
  46.   oldPtr := SYSTEM.ADR (old); newPtr := SYSTEM.ADR (new);
  47.   creationPtr := SYSTEM.ADR (creation);
  48.   ticksPerSec := Exec.execBase^.powerSupplyFrequency;
  49.   ticksPerHalf := ticksPerSec DIV 2;
  50.   ticksPerMin := ticksPerSec * 60;
  51.   ticksPerHour := ticksPerMin * 60;
  52.   ticksPerDay := ticksPerHour * 24;
  53.   FileSystem.Lookup (file, fileName, bufSize, FALSE);
  54.   IF file.res # FileSystem.done THEN (*file doesn't exist yet*)
  55.     Dos.DateStamp (creationPtr);
  56.     FileSystem.Lookup (file, fileName, bufSize, TRUE);  (*create it*)
  57.     FileSystem.WriteByteBlock (file, creation); (*write creation date*)
  58.     FileSystem.WriteByteBlock (file, uptime); (*write cummulated time = 0*)
  59.   ELSE
  60.     FileSystem.ReadByteBlock (file, creation); (*read creation date*)
  61.     FileSystem.ReadByteBlock (file, uptime); (*read old cummulated time*)
  62.   END;
  63.   Arts.Assert ((file.res = FileSystem.done) & (uptime >= 0), 
  64.                SYSTEM.ADR (fileName));
  65.   Write (ASCII.ht); WriteEsc ("32");
  66.   WriteString ("UpTime, SHML, August '90"); WriteEsc ("31"); WriteLn;
  67.   IF Arguments.NumArgs () > 0 THEN
  68.     Dos.DateStamp (oldPtr); (*get today's date*)
  69.     WriteString ("Modem has been active since ");
  70.     DEC (old.days, creation.days);
  71.     Arts.Assert (old.days >=0, SYSTEM.ADR ("Set date to today"));
  72.     WriteCard (old.days, 0); WriteString (" day(s) for ");
  73.     WriteCard (uptime DIV ticksPerHour, 0); WriteString (" hr. ");
  74.     WriteCard ((uptime MOD ticksPerHour) DIV ticksPerMin, 0); 
  75.     WriteString (" min. and ");
  76.     WriteCard ((uptime MOD ticksPerMin + ticksPerHalf) DIV ticksPerSec, 0); 
  77.     WriteString (" sec."); WriteLn;
  78.     Write (ASCII.ht); WriteEsc ("33");
  79.     WriteString ("Delete "); WriteString (fileName);
  80.     WriteString (" to restart timer"); WriteEsc ("31"); WriteLn;
  81.   ELSE
  82.     LOOP (*Break with BREAK command*)
  83.       FileSystem.Close (file);
  84.       (*make sure, the file is closed when modem is down*)
  85.       WHILE Hardware.comCD IN Hardware.ciab.pra DO (*~CD ()*)
  86.         IF Dos.ctrlC IN Exec.SetSignal (SYSTEM.LONGSET {}, 
  87.                                         SYSTEM.LONGSET {}) THEN
  88.           EXIT;
  89.         END;
  90.         Dos.Delay (delay);
  91.       END; (*wait till CD*)
  92.       (*{CD}*)
  93.       Dos.DateStamp (oldPtr);
  94.       WHILE ~(Hardware.comCD IN Hardware.ciab.pra) DO (*CD()*)
  95.         IF Dos.ctrlC IN Exec.SetSignal (SYSTEM.LONGSET {}, 
  96.                                         SYSTEM.LONGSET {}) THEN
  97.           EXIT;
  98.         END;
  99.         Dos.Delay (delay);
  100.       END; (*wait till ~CD*)
  101.       (*{~CD}*)
  102.       Dos.DateStamp (newPtr);
  103.       INC (uptime,
  104.            (new.days-old.days)*ticksPerDay+
  105.            (new.minute-old.minute)*ticksPerMin+
  106.            (new.tick-old.tick)
  107.       );
  108.       FileSystem.Lookup (file, fileName, bufSize, FALSE);
  109.       Arts.Assert (file.res = FileSystem.done, SYSTEM.ADR (fileName));
  110.       FileSystem.SetPos (file, SIZE (Dos.Date)); (*skip creation date*)
  111.       FileSystem.WriteByteBlock (file, uptime); (*write new cummulated time*)
  112.     END; (*LOOP*)
  113.   END; (*IF*)
  114. END Uptime.
  115.